///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_FONT_CFONTFACE_H_
#define _BUOLA_IMAGE_FONT_CFONTFACE_H_

#include <buola/image/cfont.h>
#include <buola/image/cfontconfig.h>
#include <buola/mat/transformations.h>

#include <ft2build.h>
#include FT_FREETYPE_H

namespace buola { namespace img {

class CScaledFont;

enum EFTExtraFlags
{
    FTEXTRA_NONE=0,
    FTEXTRA_HINT_METRICS = (1 << 0),
    FTEXTRA_EMBOLDEN = (1 << 1)
};

DECLARE_ENUM_BITOPS(EFTExtraFlags)

enum EAntialias
{
    ANTIALIAS_DEFAULT,
    ANTIALIAS_NONE,
    ANTIALIAS_GRAY,
    ANTIALIAS_SUBPIXEL
};

enum ESubpixelOrder
{
    SUBPIXEL_DEFAULT,
    SUBPIXEL_RGB,
    SUBPIXEL_BGR,
    SUBPIXEL_VRGB,
    SUBPIXEL_VBGR
};

enum EHintStyle
{
    HINTSTYLE_DEFAULT,
    HINTSTYLE_NONE,
    HINTSTYLE_SLIGHT,
    HINTSTYLE_MEDIUM,
    HINTSTYLE_FULL
};

enum EHintMetrics
{
    HINTMETRICS_DEFAULT,
    HINTMETRICS_OFF,
    HINTMETRICS_ON
};

struct CFTOptions
{
    EAntialias mAntialias;
    ESubpixelOrder mSubpixel;
    EHintStyle mHintStyle;
    EHintMetrics mHintMetrics;
    int mLoadFlags;
    EFTExtraFlags mExtraFlags;
};

struct CFTTransform
{
    double  mXScale,mYScale;
    mat::CMat23d mShape;
};

//NOTE: this is what cairo calls an unscaled_font, combined with what cairo calls a font_face... it is ok as long
//as we share FT_Face objects between font faces if they are the same

class CFontFace
{
    struct SFTFace
    {
        FT_Face mFace;

        bool mHaveScale;
        bool mHaveShape;

        FT_Matrix mCurrentFTShape;
        CFTTransform mCurrentTransform;
    };

private:
    CFontFace(FcPattern *pPattern,const CFTOptions &pOptions);

    CFontFace(FT_Face pFace,const CFTOptions &pOptions);
    CFontFace(const std::string &pFileName,int pID,const CFTOptions &pOptions);


public:
    int GetNumGlyphs();
    bool GetFontTable(uint32_t pTag,void *pDst,unsigned int &pLen);
    TGlyphIndex GetGlyph(wchar_t pC);
    int GetUnitsPerEM();
    FT_Face GetFTFace() {   return mFTFace->mFace; }
    const CFTOptions &GetFTOptions()    {   return mFTOptions;  }
    const CFTTransform &GetCurrentTransform()   {   return mFTFace->mCurrentTransform;   }
    FT_Matrix *GetCurrentFTShape()   {   return &mFTFace->mCurrentFTShape;   }

    void SetScale(const mat::C2DTransform &pSM);

    static CFontFace *GetForPattern(FcPattern *pPattern);


private:
    SFTFace *mFTFace;
    FcPattern *mPattern;

    CFTOptions mFTOptions;
};

/*namespace img*/ } /*namespace buola*/ }

#endif
